﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AGVController.BaseClass
{
    public class Common
    {
        private static ushort number = 0;
        public static T bytesToStructure<T>(byte[] bytesBuffer)
        {
            if (bytesBuffer.Length < Marshal.SizeOf(typeof(T)))
            {
                throw new ArgumentException("size error");
            }

            IntPtr bufferHandler = Marshal.AllocHGlobal(bytesBuffer.Length);
            for (int index = 0; index < bytesBuffer.Length; index++)
            {
                Marshal.WriteByte(bufferHandler, index, bytesBuffer[index]);
            }
            T structObject = (T)Marshal.PtrToStructure(bufferHandler, typeof(T));
            Marshal.FreeHGlobal(bufferHandler);
            return structObject;
        }

        public static byte[] seerMessageHeadToBytes(SeerMessageHead msg)
        {


            var hsize = Marshal.SizeOf(msg);
            byte[] bytes = new byte[hsize];
            IntPtr structPtr = Marshal.AllocHGlobal(hsize);
            Marshal.StructureToPtr(msg, structPtr, false);
            Marshal.Copy(structPtr, bytes, 0, hsize);
            Marshal.FreeHGlobal(structPtr);
            return bytes;


        }

        public static SeerMessageHead getSeerMessageHead(ushort type, uint length)
        {
            SeerMessageHead smh = new SeerMessageHead();
            smh.sync = 0x5A;
            smh.version = 0x01;
            smh.number = number > 65533 ? (ushort)0 : number++;
            smh.length = length;
            smh.type = type;
            return smh;
        }
        public static byte[] hexStrTobyte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
            return returnBytes;
        }

        public static byte[] normalStrToHexByte(string str)
        {
            byte[] result = new byte[str.Length];

            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
            for (int i = 0; i < buffer.Length; i++)
            {
                result[i] = Convert.ToByte(buffer[i].ToString("X2"), 16);
            }
            return result;
        }


        public static List<string> Robot_Push_included_fields = new List<string> {
            "x",
            "y",
            "angle",
            "current_station",
            "blocked",
            "battery_level",
            "charging",
            "emergency",
            "fatals",
            "errors",
            "warnings",
            "soft_emc",
            "reloc_status",
            "task_status",
            "task_type",
            "target_id"
        };
    }
    [StructLayout(LayoutKind.Sequential, Pack = 1)]  //顺序排列，并按1字节对齐 （这里保存的是一组飞机参数,共73字节）
    public struct SeerMessageHead
    {
        public byte sync;
        public byte version;
        public UInt16 number;
        public UInt32 length;
        public UInt16 type;
        private byte ref0;      //保留
        private byte ref1;      //保留
        private byte ref2;      //保留
        private byte ref3;      //保留
        private byte ref4;      //保留
        private byte ref5;      //保留

    };
    public struct SeerMessage
    {
        public SeerMessageHead head;
        public byte[] data;
        public int length()
        {
            return data.Length + Marshal.SizeOf(head);
        }
    }
}
